import Vue from 'vue';
import VueX from 'vuex'
Vue.use(VueX);
export default new VueX.Store({
  state: {
    hospitalList: '',
    transferOrder: '',
    centerId: '',
    taskList: '',
    remoteTransferOrderList: [],
    transferOrderDetail: '',
    clickedPosition: '',
    hospitalFilterList: '',
    ambGPSList: '',
    ambStateList: '',
    toldInfo: '',
    hospitalIndexList: '',
    checkedHospital: '',
    giveOxyWayList:'',
    infuseWayList:''
  },
  actions: {
    SaveHospitalList(ctx, data) {
      ctx.commit('SaveHospitalList', data)
    },
    SaveTaskList(ctx, data) {
      ctx.commit("SaveTaskList", data);
    },
    SaveRemoteTransferOrderList(ctx, data) {
      ctx.commit("SaveRemoteTransferOrderList", data);
    },
    RemoveTransferOrder(ctx, data) {
      ctx.commit("RemoveTransferOrder", data);
    },
    SaveTransferOrderDetail(ctx, data) {
      ctx.commit("SaveTransferOrderDetail", data);
    },
    SortHospital(ctx, data) {
      ctx.commit("SortHospital", data);
    },
    SaveAmbGPSList(ctx, data) {
      ctx.commit("SaveAmbGPSList", data);
    },
    SaveAmbStateList(ctx, data) {
      ctx.commit("SaveAmbStateList", data);
    },
    SaveToldInfo(ctx, data) {
      ctx.commit("SaveToldInfo", data);
    },
    SaveGiveOxyWayList(ctx, data){
      ctx.commit("SaveGiveOxyWayList", data);
    },
    SaveInfuseWayList(ctx, data){
      ctx.commit("SaveInfuseWayList", data);
    }
  },
  mutations: {
    SaveHospitalList(state, data) {
      state.hospitalIndexList = state.hospitalFilterList = state.hospitalList = data;
    },
    SaveClickedTransferOrder(state, data) {
      state.transferOrder = data;
    },
    SaveTaskList(state, data) {
      state.taskList = data;
    },
    SaveClickedCenterId(state, data) {
      state.centerId = data;
    },
    SaveRemoteTransferOrderList(state, data) {
      state.remoteTransferOrderList.push(data);
    },
    RemoveTransferOrder(state, data) {
      let index = state.remoteTransferOrderList.findIndex(item => item.transferOrder == data);
      if (index == -1) {
        return;
      }
      state.remoteTransferOrderList.splice(index, 1);
    },
    SaveTransferOrderDetail(state, data) {
      state.transferOrderDetail = data
    },
    ClearTransferOrderDetail(state) {
      state.transferOrderDetail = '';
    },
    SaveClickedPosition(state, data) {
      state.clickedPosition = data;
    },
    SortHospital(state, data) {
      let list = state.hospitalList;
      let i = -1;
      state.hospitalList = list.map(q => {
        if (q.latitude == 0 || q.longitude == 0) {
          return {
            ...q,
            distance: 9999999999,
            duration: 9999999999
          }
        } else {
          i++;
          return {
            ...q,
            distance: data[i].distance.value,
            duration: data[i].duration.vaule
          }
        }
      }).sort((a, b) => a.distance - b.distance);
      state.hospitalFilterList = state.hospitalList;
    },
    FilterHospital(state, data) {
      if (data == '') {
        state.hospitalFilterList = state.hospitalList;
      } else {
        state.hospitalFilterList = state.hospitalList.filter(q => q.name.includes(data));
      }
    },
    FilterIndexHospital(state, data) {
      if (data == '') {
        state.hospitalIndexList = state.hospitalList;
      } else {
        state.hospitalIndexList = state.hospitalList.filter(q => q.name.includes(data));
      }
    },
    SaveAmbGPSList(state, data) {
      state.ambGPSList = data;
    },
    SaveAmbStateList(state, data) {
      state.ambStateList = data;
    },
    SaveToldInfo(state, data) {
      state.toldInfo = data;
    },
    SaveCheckedHospital(state, data) {
      state.checkedHospital = data;
    },
    SaveGiveOxyWayList(state, data) {
      state.giveOxyWayList = data;
    },
    SaveInfuseWayList(state, data) {
      state.infuseWayList = data;
    }
  }
})
